package com.ad.pet.app.mall;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.mall.adapter.CounpousAdapter;
import com.ad.pet.app.shopcar.confirm.ConfirmOrderContracts;
import com.ad.pet.app.shopcar.confirm.ConfirmOrderPresenter;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.models.remote.account.Banlance;
import com.ad.pet.models.remote.account.UserBuyReturn;
import com.ad.pet.models.remote.account.order.PayCompleted;
import com.ad.pet.models.remote.account.order.SignCheck;
import com.ad.pet.models.remote.cart.BigShipingw;
import com.ad.pet.models.remote.cart.Coupons;
import com.ad.pet.models.remote.mall.Counpous;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liuyixiang on 2019/12/4.
 */

public class ChoseCounpousActivity extends AppToolbarActivity implements ConfirmOrderContracts.View {

    @BindView(R.id.recycler_counpus)
    RecyclerView recyclerCounpus;

    @BindView(R.id.tv_count)
    TextView tvCount;

    @Inject
    ConfirmOrderPresenter confirmOrderPresenter;
    private CounpousAdapter couponsAdaper;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
        mToolbar.setTitle("选择优惠券");
        mToolbar.setNextText("不使用");
        mToolbar.setNextTextColor(getResources().getColor(R.color.black3d));
        mToolbar.setNextOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Counpous counpous = new Counpous();
                counpous.setVoucher_id("");
                counpous.setVoucher_name("");
                Intent intent = new Intent();
                intent.putExtra("youhuiquan",counpous);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    protected int contentLayout() {
        return R.layout.item_chose_counpous_activity;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        confirmOrderPresenter.onCreate(this);

        couponsAdaper = new CounpousAdapter(null);
        recyclerCounpus.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerCounpus.setAdapter(couponsAdaper);
        couponsAdaper.bindToRecyclerView(recyclerCounpus);
    }

    @Override
    protected void initData() {
        String state = getIntent().getStringExtra("state");
        String week = getIntent().getStringExtra("week");
        confirmOrderPresenter.getCoupons("",state,week);
    }

    @Override
    protected void initEvents() {

        couponsAdaper.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Counpous counpous = couponsAdaper.getItem(position);
                Intent intent = new Intent();
                if (Double.valueOf(counpous.getDiscount())>0){
                    String zhekou = Double.valueOf(counpous.getDiscount())*10+"";
                    zhekou = zhekou.replace(".0","");
                    counpous.setVoucher_name("折扣券 "+zhekou+"折 "+counpous.getVoucher_name());
                }else {
                    counpous.setVoucher_name("抵用券 "+counpous.getVoucher_name());
                }
                intent.putExtra("youhuiquan",counpous);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getCouponsSuccess(List<Counpous> coupons) {

        if (coupons.size()==0){
            couponsAdaper.setEmptyView(R.layout.layout_empty_address);
            tvCount.setVisibility(View.GONE);
            ((TextView) couponsAdaper.getEmptyView().findViewById(R.id.tv_title)).setText("暂无优惠券");
        }else {
            couponsAdaper.setNewData(coupons);
        }

        tvCount.setText("共"+coupons.size()+"张可用优惠券");
    }

    @Override
    public void checkCouponsSuccess(Coupons coupons) {

    }

    @Override
    public void confirmOrderSuccess(PayCompleted shop) {

    }

    @Override
    public void getUserBanceSuccess(Banlance banlance) {

    }

    @Override
    public void getShipingWaysSuccess(BigShipingw shippingWay) {

    }

    @Override
    public void paymentBalanceSuccess(int toPay) {

    }

    @Override
    public void checkIsSignSuccess(SignCheck signCheck) {

    }

    @Override
    public void daySignSuccess(SignCheck signCheck) {

    }

    @Override
    public void sharH5UrlSuccess(SignCheck signCheck) {

    }

    @Override
    public void get_user_buy_return_success(UserBuyReturn userBuyReturn) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
